package org.test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
	
	import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectTableReference;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitorAdapter;
import com.alibaba.druid.util.JdbcConstants;

public class App {
	public static void main(String[] args) throws Exception {
		final String dbType = JdbcConstants.ORACLE; // JdbcConstants.MYSQL或者JdbcConstants.POSTGRESQL
		String sql = "select * from mytable a ,tab2 b where a.id = 3";
		List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);

		ExportTableAliasVisitor visitor = new ExportTableAliasVisitor();
		for (SQLStatement stmt : stmtList) {
			stmt.accept(visitor);
		}

		System.out.println(visitor.getAliasMap());
		
	}

	public static class ExportTableAliasVisitor extends OracleASTVisitorAdapter {
		private Map<String, SQLTableSource> aliasMap = new HashMap<String, SQLTableSource>();

		public boolean visit(OracleSelectTableReference x) {
			System.out.println(x.getClass());
			String alias = x.getAlias();
			aliasMap.put(alias, x);
			return true;
		}

		public Map<String, SQLTableSource> getAliasMap() {
			return aliasMap;
		}
	}
}